<template>
<h1>注册页面</h1>
<el-card style="width: 450px;margin: 0 auto; padding:20px 10px 0 0;">
  <el-form label-width="60px">
    <el-form-item label="用户名">
      <el-input placeholder="请输入用户名" v-model="user.username"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input placeholder="请输入密码" type="password" v-model="user.password"></el-input>
    </el-form-item>
    <el-form-item label="昵称">
      <el-input placeholder="请输入昵称" v-model="user.nickname"></el-input>
    </el-form-item>
    <el-form-item>
     <el-button type="primary" style="width: 100%" @click="reg()">注册</el-button>
    </el-form-item>
  </el-form>
</el-card>
</template>

<script setup>
import {ref} from "vue";
import axios from "axios";//注意:axios必须导入
import {ElMessage} from "element-plus";
import router from "@/router";
import qs from "qs";//注意:qs必须导入

const user=ref({username:'',password:'',nickname:''});
const reg=()=>{
  // console.log(user.value);
  //qs.stringify将JS对象转为查询字符串的格式
  let data=qs.stringify(user.value);
  console.log(data);
  //给后端发起注册的请求
  axios.post('http://localhost:8080/v1/users/reg',data)
      .then((response) => {//回调函数
        //回调函数:拿到服务器响应才执行此处代码,response.data里是服务器响应的内容
        //如果响应状态码是2002提示成功消息
        if (response.data.code==2002){
          ElMessage.success('注册成功');
          //再跳转至首页
          router.push('/');
        }else {
          //反之打印服务器响应的错误消息文本
          ElMessage.error(response.data.msg)
        }
      });
}
</script>

<style scoped>

</style>